﻿package view.message
{
    import com.core.display.*;
    import com.greensock.*;
    import flash.display.*;
    import flash.events.*;
    import morn.core.handlers.*;

    public class Alert extends AlertUI
    {
        private var callBack:Function;
        private var args:Object;
        private var _text:TextField;
        private var _text2:TextField;
        private var _line:Bitmap;
        private var _model:Boolean = false;
        private var _autoclose:int = 0;
        private var _intvalid:uint = 0;
        private var _btns:Array;
        private var _tween:TweenLite;
        public static var CLOSE:int = 99;
        private static var _parent:DisplayObjectContainer;

        public function Alert()
        {
            return;
        }// end function

        private function initView(param1:String, param2:String, param3:Array, param4:Function, param5:Boolean, param6:Boolean) : void
        {
            this._model = param6;
            this._btns = param3;
            this.closeBtn.visible = param5;
            if (param5)
            {
                closeBtn.clickHandler = new Handler(this.clickHandler3);
            }
            this.textField.text = param1;
            this.title.text = param2;
            if (param3 == null)
            {
                param3 = ["确定"];
            }
            btn1.visible = false;
            btn2.visible = false;
            if (param3.length >= 1)
            {
                btn1.visible = true;
                btn1.x = 87;
                btn1.label = param3[0];
                this.btn1.clickHandler = new Handler(this.clickHandler);
            }
            if (param3.length == 2)
            {
                btn2.visible = true;
                btn2.x = 144;
                btn1.x = 29;
                btn2.label = param3[1];
                this.btn2.clickHandler = new Handler(this.clickHandler2);
            }
            this.callBack = param4;
            _parent.stage.addEventListener(Event.RESIZE, this.resizeHandler);
            return;
        }// end function

        private function clickHandler3() : void
        {
            if (this.callBack != null)
            {
                this.callBack(CLOSE, this.args);
            }
            if (_parent.contains(this))
            {
                _parent.removeChild(this);
            }
            this.destroy();
            return;
        }// end function

        public function drawModel() : void
        {
            if (this._model)
            {
                this.graphics.clear();
                this.graphics.beginFill(0, 0.3);
                this.graphics.drawRect(-this.x, -this.y, _parent.stage.stageWidth, _parent.stage.stageHeight);
                this.graphics.endFill();
            }
            return;
        }// end function

        protected function resizeHandler(event:Event) : void
        {
            if (this._model)
            {
                this.drawModel();
            }
            return;
        }// end function

        protected function clickHandler() : void
        {
            if (this.callBack != null)
            {
                this.callBack(0, this.args);
            }
            if (_parent.contains(this))
            {
                _parent.removeChild(this);
            }
            this.destroy();
            return;
        }// end function

        protected function clickHandler2() : void
        {
            if (this.callBack != null)
            {
                this.callBack(1, this.args);
            }
            if (_parent.contains(this))
            {
                _parent.removeChild(this);
            }
            this.destroy();
            return;
        }// end function

        protected function destroy() : void
        {
            this.callBack = null;
            this.args = null;
            this._model = false;
            this._btns = null;
            if (this._tween)
            {
                this._tween.kill();
                this._tween = null;
            }
            if (btn1 && btn1.clickHandler)
            {
                this.btn1.clickHandler.dispose();
                this.btn1.clickHandler = null;
            }
            if (btn2 && btn2.clickHandler)
            {
                this.btn2.clickHandler.dispose();
                this.btn2.clickHandler = null;
            }
            if (closeBtn && closeBtn.clickHandler)
            {
                this.closeBtn.clickHandler.dispose();
                this.closeBtn.clickHandler = null;
            }
            return;
        }// end function

        public function tweenShow() : void
        {
            this._tween = TweenLite.to(this, 0.2, {alpha:1});
            return;
        }// end function

        public static function init(param1:Object) : void
        {
            _parent = DisplayObjectContainer(param1);
            return;
        }// end function

        public static function show(param1:String, param2:String, param3:Array = null, param4:Function = null, param5:Object = null, param6:Boolean = false, param7:Boolean = true) : Alert
        {
            var _loc_8:* = new Alert;
            new Alert.args = param5;
            _loc_8.initView(param1, param2, param3, param4, param6, param7);
            _parent.addChild(_loc_8);
            _loc_8.x = _parent.stage.stageWidth / 2 - _loc_8.width / 2;
            _loc_8.y = _parent.stage.stageHeight / 2 - _loc_8.height / 2;
            _loc_8.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
            _loc_8.addEventListener(MouseEvent.MOUSE_UP, mouseDown);
            _loc_8.alpha = 0;
            _loc_8.tweenShow();
            if (param7)
            {
                _loc_8.drawModel();
            }
            return _loc_8;
        }// end function

        static function mouseDown(event:MouseEvent) : void
        {
            event.stopImmediatePropagation();
            return;
        }// end function

    }
}
